﻿Imports System
Imports System.CodeDom
Imports System.Globalization
Imports System.Reflection

Namespace System.Workflow.ComponentModel.Compiler
    Friend Class DesignTimeMethodInfo
        Inherits MethodInfo
        ' Methods
        Friend Sub New(ByVal declaringType As DesignTimeType, ByVal methodInfo As CodeMemberMethod)
            Me.declaringType = declaringType
            Me.methodInfo = methodInfo
        End Sub

        Friend Sub New(ByVal declaringType As DesignTimeType, ByVal methodInfo As CodeMemberMethod, ByVal isSpecialName As Boolean)
            Me.declaringType = declaringType
            Me.methodInfo = methodInfo
            Me.isSpecialName = isSpecialName
        End Sub

        Public Overrides Function GetBaseDefinition() As MethodInfo
            Throw New NotImplementedException
        End Function

        Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
            Return Me.GetCustomAttributes(GetType(Object), inherit)
        End Function

        Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
            If (attributeType Is Nothing) Then
                Throw New ArgumentNullException("attributeType")
            End If
            If (Me.attributes Is Nothing) Then
                If (Me.methodInfo Is Nothing) Then
                    Me.attributes = New Attribute(0  - 1) {}
                Else
                    Me.attributes = Helper.LoadCustomAttributes(Me.methodInfo.CustomAttributes, [TryCast](Me.DeclaringType,DesignTimeType))
                End If
            End If
            Return Helper.GetCustomAttributes(attributeType, inherit, Me.attributes, Me)
        End Function

        Public Overrides Function GetMethodImplementationFlags() As MethodImplAttributes
            Return MethodImplAttributes.IL
        End Function

        Public Overrides Function GetParameters() As ParameterInfo()
            If (Me.parameters Is Nothing) Then
                Dim parameters As CodeParameterDeclarationExpressionCollection = Me.methodInfo.Parameters
                Dim infoArray As ParameterInfo() = New ParameterInfo(parameters.Count  - 1) {}
                Dim i As Integer
                For i = 0 To parameters.Count - 1
                    infoArray(i) = New DesignTimeParameterInfo(parameters.Item(i), i, Me)
                Next i
                Me.parameters = infoArray
            End If
            Return Me.parameters
        End Function

        Public Overrides Function Invoke(ByVal obj As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal parameters As Object(), ByVal culture As CultureInfo) As Object
            Throw New NotImplementedException(TypeSystemSR.GetString("Error_RuntimeNotSupported"))
        End Function

        Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
            If (attributeType Is Nothing) Then
                Throw New ArgumentNullException("attributeType")
            End If
            If (Me.attributes Is Nothing) Then
                Me.attributes = Helper.LoadCustomAttributes(Me.methodInfo.CustomAttributes, [TryCast](Me.DeclaringType,DesignTimeType))
            End If
            Return Helper.IsDefined(attributeType, inherit, Me.attributes, Me)
        End Function


        ' Properties
        Public Overrides ReadOnly Property Attributes As MethodAttributes
            Get
                Return (Helper.ConvertToMethodAttributes(Me.methodInfo.Attributes) Or If(Me.isSpecialName, &H800, 0))
            End Get
        End Property

        Public Overrides ReadOnly Property DeclaringType As Type
            Get
                Return Me.declaringType
            End Get
        End Property

        Public Overrides ReadOnly Property MethodHandle As RuntimeMethodHandle
            Get
                Throw New NotImplementedException(TypeSystemSR.GetString("Error_RuntimeNotSupported"))
            End Get
        End Property

        Public Overrides ReadOnly Property Name As String
            Get
                Return Helper.EnsureTypeName(Me.methodInfo.Name)
            End Get
        End Property

        Public Overrides ReadOnly Property ReflectedType As Type
            Get
                Return Me.declaringType
            End Get
        End Property

        Public Overrides ReadOnly Property ReturnParameter As ParameterInfo
            Get
                If (Me.returnParam Is Nothing) Then
                    Me.returnParam = New DesignTimeParameterInfo(Me.methodInfo.ReturnType, Me)
                End If
                Return Me.returnParam
            End Get
        End Property

        Public Overrides ReadOnly Property ReturnType As Type
            Get
                Return Me.declaringType.ResolveType(DesignTimeType.GetTypeNameFromCodeTypeReference(Me.methodInfo.ReturnType, Me.declaringType))
            End Get
        End Property

        Public Overrides ReadOnly Property ReturnTypeCustomAttributes As ICustomAttributeProvider
            Get
                Return Nothing
            End Get
        End Property


        ' Fields
        Private attributes As Attribute()
        Private declaringType As DesignTimeType
        Private isSpecialName As Boolean
        Private methodInfo As CodeMemberMethod
        Private parameters As ParameterInfo()
        Private returnParam As ParameterInfo
    End Class
End Namespace

